package at.jku.ce.georg;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.xml.namespace.QName;

import at.jku.ce.services.impl.BOMServiceImpl;
import at.jku.ce.services.impl.BOMServiceImplService;

public class GUI {
	protected static GUI instance = null;
	protected BOMServiceImpl port=null;
	
	protected ArrayList<String> logMsgs = new ArrayList<String>();

	private static final QName SERVICE_NAME = new QName(
			"http://impl.services.ce.jku.at/", "BOMServiceImplService");

	protected GUI() {
	}

	public static GUI getInstance() {
		if (instance == null)
			instance = new GUI();
		return instance;
	}

	public void connectTo(String wsdlLocation) {
		URL wsdlURL = null;
		if (wsdlLocation != null) {
			File wsdlFile = new File(wsdlLocation);
			try {
				if (wsdlFile.exists()) {
					wsdlURL = wsdlFile.toURI().toURL();
				} else {
					wsdlURL = new URL(wsdlLocation);
				}
			} catch (MalformedURLException e) {
				e.printStackTrace();
			}
		}
		connectTo(wsdlURL);
	}

	public void connectTo(URL wsdlURL) {
		if (wsdlURL == null)
			wsdlURL = BOMServiceImplService.WSDL_LOCATION;

		BOMServiceImplService ss = new BOMServiceImplService(wsdlURL,
				SERVICE_NAME);
		port = ss.getBOMServiceImplPort();
		log("connected: " + wsdlURL.toString()+" <br />");
	}
	
	public List<String> callGetAllParts(){
		if(port!=null) {
			return port.getAllParts();
		}
		List<String> result = new ArrayList<String>();
		result.add("no port");
		result.add("connect first!");
		log("error: no port; connect first");
		return result;
	}

	public List<String> callGetDirectSubPartsOf(String arg0){
		if(port!=null) {
			return port.getDirectSubPartsOf(arg0);
		}
		List<String> result = new ArrayList<String>();
		result.add("no port");
		result.add("connect first!");
		log("error: no port; connect first");
		return result;
	}

	public List<String> callSetDirectSubPartsOf(String arg0, List<String> arg1){
		if(port!=null) {
			port.setDirectSubPartsOf(arg0, arg1);
			List<String> result = new ArrayList<String>();
			result.add("done");
			log("setDirectSubPartsOf done");
			return result;
		} else {
			List<String> result = new ArrayList<String>();
			result.add("no port");
			result.add("connect first!");
			log("error: no port; connect first");
			return result;
		}
	}

	public List<String> callCopyTo(String url2){
		if(port!=null && url2!=null && !url2.isEmpty()) {
			List<String> result = new ArrayList<String>();
			
			BOMServiceImplService otherservice;
			try {
				otherservice = new BOMServiceImplService(new URL(url2),SERVICE_NAME);
			BOMServiceImpl otherport = otherservice.getBOMServiceImplPort();
			if(otherport!=null){
				for(String part : port.getAllParts()){
					result.add(part+":");
					for(String subpart : port.getDirectSubPartsOf(part)){
						result.add("->" + subpart);
					}
					otherport.setDirectSubPartsOf(part, port.getDirectSubPartsOf(part));
				}
			}
			result.add("done");
			log("done: callCopyTo: "+url2);
			} catch (MalformedURLException e) {
				log("ERROR: " + e.getLocalizedMessage());
			}
			return result;
		} else {
			List<String> result = new ArrayList<String>();
			result.add("no port");
			result.add("connect first!");
			log("error: no port; connect first");
			return result;
		}
	}

	
	public void log(String logtxt) {
		if(logMsgs.size()>=100) {
			logMsgs.remove(0);
		}
		logMsgs.add(logtxt);
	}
	
	public List<String> getLog(){
		return logMsgs;
	}

}
